<template>
  <div class="login-container">
    <el-form
      ref="loginForm"
      :model="loginForm"
      :rules="loginRules"
      class="login-form"
      auto-complete="on"
      label-position="left"
      v-if="loginType==='1'"
    >
      <div style="text-align:center;margin-top:100px;">
        <img
          src="../../assets/logo.png"
          alt=""
        >
      </div>
      <h3 class="title">量准Ration后台管理系统</h3>
      <el-form-item prop="username">
        <span class="svg-container">
          <svg-icon icon-class="user" />
        </span>
        <el-input
          v-model="loginForm.username"
          name="username"
          type="text"
          auto-complete="on"
          placeholder="手机号"
        />
      </el-form-item>
      <el-form-item prop="password">
        <span class="svg-container">
          <svg-icon icon-class="password" />
        </span>
        <el-input
          :type="pwdType"
          v-model="loginForm.password"
          name="password"
          auto-complete="on"
          placeholder="密码为6-12位数字或字母"
          @keyup.enter.native="handleLogin"
        />
        <span
          class="show-pwd"
          @click="showPwd"
        >
          <svg-icon icon-class="eye" />
        </span>
      </el-form-item>
      <el-form-item>
        <el-button
          :loading="loading"
          type="primary"
          style="width:100%;"
          @click.native.prevent="handleLogin"
        >
          登&nbsp;&nbsp;&nbsp;&nbsp;录
        </el-button>
      </el-form-item>
      <div class="find-pwd">
        <el-button
          type="text"
          class="find-pwd-btn"
          @click="loginType='3'"
        >找回密码</el-button>
      </div>

      <div class="login-bottom">
        <el-button
          type="text"
          class="login-text"
          @click="loginType = '2'"
        >注册新用户</el-button>
      </div>
    </el-form>

    <el-form
      ref="rePasswordForm"
      :model="rePassword"
      :rules="loginRules"
      class="login-form"
      auto-complete="on"
      label-position="left"
      v-if="loginType==='2'||loginType==='3'"
    >
      <div style="text-align:center;margin-top:100px;">
        <img
          src="../../assets/logo.png"
          alt=""
        >
      </div>
      <h3 class="title">量准SPR后台管理系统</h3>
      <el-form-item prop="username">
        <el-input
          v-model="rePassword.username"
          name="username"
          type="text"
          auto-complete="on"
          placeholder="手机号"
        />
      </el-form-item>
      <el-form-item
        prop="code"
        style="float:left;width:200px;"
      >
        <el-input
          v-model="rePassword.code"
          name="code"
          type="text"
          placeholder="验证码"
        ></el-input>
      </el-form-item>
      <el-button
        type="text"
        @click="getCode"
        class="get-code"
        v-show="!computedTime"
      >获取验证码</el-button>
      <el-button
        type="text"
        @click="getCode"
        class="get-code"
        v-show="computedTime"
      >已发送({{computedTime}}s)</el-button>
      <div style="clear:both;"></div>
      <el-form-item prop="password">
        <el-input
          :type="pwdType"
          v-model="rePassword.password"
          name="password"
          placeholder="密码为6-12位数字或字母"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          :loading="loading"
          type="primary"
          style="width:100%;"
          @click.native.prevent="handleSubmit('rePasswordForm')"
          v-if="loginType==='2'"
        >
          注&nbsp;&nbsp;&nbsp;&nbsp;册
        </el-button>
        <el-button
          :loading="loading"
          type="primary"
          style="width:100%;"
          @click.native.prevent="handleSubmit('rePasswordForm')"
          v-if="loginType==='3'"
        >
          重置密码
        </el-button>
      </el-form-item>

      <div class="login-bottom">
        <el-button
          type="text"
          class="login-text"
          @click="loginType = '1'"
        >登录</el-button>
        <el-button
          type="text"
          class="login-text"
          @click="loginType = '3'"
        >找回密码</el-button>
      </div>
    </el-form>

  </div>
</template>

<script>
import { isvalidUsername } from "@/utils/validate";
import { login, register, fetchCode, resetPwd } from "@/api/login";
import md5 from "md5";
import { setStore } from "@/utils/common";

export default {
  name: "Login",
  data() {
    const validateUsername = (rule, value, callback) => {
      if (value.length !== 11) {
        callback(new Error("请填写正确手机号"));
      } else {
        callback();
      }
    };
    const validatePass = (rule, value, callback) => {
      if (value.length < 6) {
        callback(new Error("密码不能小于6位"));
      } else {
        callback();
      }
    };
    return {
      loginForm: {
        username: "",
        password: ""
      },
      rePassword: {
        username: "",
        code: "",
        password: ""
      },
      loginRules: {
        username: [
          { required: true, trigger: "blur", validator: validateUsername }
        ],
        password: [
          { required: true, trigger: "blur", validator: validatePass }
        ],
        code: [{ required: true, trigger: "blur", message: "请填写验证码" }]
      },
      loading: false,
      pwdType: "password",
      redirect: undefined,
      computedTime: 0,
      timer: 0,
      loginType: "1"
    };
  },
  watch: {
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect;
      },
      immediate: true
    }
  },
  mounted() {},
  methods: {
    showPwd() {
      if (this.pwdType === "password") {
        this.pwdType = "";
      } else {
        this.pwdType = "password";
      }
    },
    async handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          let formData = {};
          // this.loading = true;
          formData.password = this.loginForm.password;
          formData.userName = this.loginForm.username;
          this.$store
            .dispatch("Login", formData)
            .then(() => {
              this.loading = false;
              this.$router.push({ path: this.redirect || "/" });
            })
            .catch(() => {
              this.loading = false;
            });
        } else {
          return false;
        }
      });
    },
    async getCode() {
      if (this.rePassword.username.length == 11) {
        this.computedTime = 60;
        this.timer = setInterval(() => {
          this.computedTime--;
          if (this.computedTime == 0) {
            clearInterval(this.timer);
          }
        }, 1000);
        const res = await fetchCode({
          tel: this.rePassword.username
        });
        this.$message.success("短息发送成功");
      }
    },
    async fetchResetPwd(formName) {
      const res = await resetPwd({
        userName: this.rePassword.username,
        password: this.rePassword.password,
        code: this.rePassword.code
      });
      this.$message.success("找回成功");
      this.resetForm(formName);
    },
    handleSubmit(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          if (this.loginType === "2") {
            this.fetchRegister(formName);
          } else if (this.loginType === "3") {
            this.fetchResetPwd(formName);
          }
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    async fetchRegister(formName) {
      const res = await register({
        userName: this.rePassword.username,
        password: this.rePassword.password,
        code: this.rePassword.code
      });
      this.$message.success("注册成功");
      this.resetForm(formName);
    }
  }
};
</script>

<style rel="stylesheet/scss" lang="scss">
$bg: #2d3a4b;
$light_gray: #eee;

/* reset element-ui css */
.login-container {
  .el-input {
    display: inline-block;
    height: 47px;
    width: 85%;
    input {
      background: transparent;
      border: 0px;
      -webkit-appearance: none;
      border-radius: 0px;
      padding: 12px 5px 12px 15px;
      color: $light_gray;
      height: 47px;
      &:-webkit-autofill {
        -webkit-box-shadow: 0 0 0px 1000px $bg inset !important;
        -webkit-text-fill-color: #fff !important;
      }
    }
  }
  .el-form-item {
    border: 1px solid rgba(255, 255, 255, 0.1);
    background: rgba(0, 0, 0, 0.1);
    border-radius: 5px;
    color: #454545;
  }
}
</style>

<style rel="stylesheet/scss" lang="scss" scoped>
$bg: #2d3a4b;
$dark_gray: #889aa4;
$light_gray: #eee;
.login-container {
  position: fixed;
  height: 100%;
  width: 100%;
  background: url("../../assets/login-bg.jpg") no-repeat;
  .login-form {
    position: absolute;
    right: 0;
    width: 520px;
    height: 100%;
    max-width: 100%;
    padding: 35px 35px 15px 35px;
    background: rgba(0, 0, 0, 0.7);
  }
  .tips {
    font-size: 14px;
    color: #fff;
    margin-bottom: 10px;
    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }
  .svg-container {
    padding: 6px 5px 6px 15px;
    color: $dark_gray;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
  }
  .title {
    font-size: 26px;
    font-weight: 400;
    color: $light_gray;
    margin: 0px auto 40px auto;
    text-align: center;
    font-weight: bold;
  }
  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }
}

.find-pwd {
  text-align: center;
  &-btn {
    color: #fff;
  }
}
.login-bottom {
  position: absolute;
  bottom: 0;
  left: 0;
  right: 0;
  text-align: center;
}
.login-text {
  color: #fff;
  font-size: 20px;
}
.get-code {
  float: left;
  margin-left: 100px;
  text-align: center;
}
</style>
